﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;

namespace Skynet.WebAdministracion.Views.ImageMgm
{
    public class ImageViewPresenter : Infrastructure.Web.Views.Master.MasterPresenter<IImageMgmView>
    {
        public override void OnViewInitialized()
        {
            base.OnViewInitialized();
        }
        public override void OnViewLoaded()
        {
            base.OnViewLoaded();
        }
        private CloudBlobContainer CreateBlobStorage(string containername)
        {
            var storageAccount = CloudStorageAccount.FromConfigurationSetting("InfrastructureConnectionString");
            CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobStorage.GetContainerReference(containername);
            bool created = container.CreateIfNotExist();
            if (created)
            {
                var permissions = container.GetPermissions();
                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                container.SetPermissions(permissions);
            }
            return container;
        }
        public void SaveImage(string name ,    byte[] image)
        {
            var imageType = System.IO.Path.GetExtension(name);
            var blobcontainer = this.CreateBlobStorage("photos");
            string uniqueBlobName = string.Format("image_{0}", name);
            CloudBlockBlob blob = blobcontainer.GetBlockBlobReference(uniqueBlobName);
            blob.Properties.ContentType = imageType;
            blob.UploadByteArray(image);             
        }
        


    }
}
